package com.r2coding.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.r2coding.music.dao.SingerCommentDao;
import com.r2coding.music.entity.SingerCommentEntity;
import com.r2coding.music.feign.SystemFeignService;
import com.r2coding.music.model.CommentUser;
import com.r2coding.music.service.SingerCommentService;
import com.r2coding.music.to.User;
import com.r2coding.music.vo.resp.SingerCommentRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:98k灬
 * @since:2022/05/30 13:23
 **/
@Service("singerCommentService")
public class SingerCommentServiceImpl extends ServiceImpl<SingerCommentDao, SingerCommentEntity> implements SingerCommentService {
    @Autowired
    SystemFeignService systemFeignService;

    @Override
    public void saveComment(SingerCommentEntity comment) {
        // 根据评论用户id查询评论用户信息
        User commentUser = systemFeignService.getById(comment.getCommentUserId());
        if (commentUser != null) {
            comment.setCommentUserAvatar(commentUser.getAvatar());
            comment.setCommentUserName(commentUser.getUserName());
        }

        // 根据被评论用户id查询被评论用户信息
        User targetUser = systemFeignService.getById(comment.getTargetUserId());
        if (targetUser != null) {
            comment.setTargetUserAvatar(targetUser.getAvatar());
            comment.setTargetUserName(targetUser.getUserName());
        }

        comment.setCreateTime(new Date());
        this.baseMapper.insert(comment);
    }

    @Override
    public List<SingerCommentRespVo> getAll(Integer id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<SingerCommentRespVo> respVos = new ArrayList<>();
        QueryWrapper<SingerCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("singer_id", id);
        wrapper.orderByDesc("create_time");
        List<SingerCommentEntity> commentEntities = this.baseMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(commentEntities)) {
            respVos = commentEntities.stream().map( item -> {
                SingerCommentRespVo respVo = new SingerCommentRespVo();
                BeanUtils.copyProperties(item, respVo);
                respVo.setCreateDate(sdf.format(item.getCreateTime()));
                return respVo;
            }).collect(Collectors.toList());
        }

        List<SingerCommentRespVo> list = new ArrayList<>();
        for (SingerCommentRespVo entity : respVos) {
            if (entity.getParentId().equals(0)) {
                list.add(entity);
            }
        }
        for (SingerCommentRespVo entity : list) {
            childrenComment(respVos, entity);
        }
        this.childrenList(list);
        return list;
    }

    private List<SingerCommentRespVo> childrenList(List<SingerCommentRespVo> list) {
        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                // 封装评论用户的用户信息
                CommentUser commentUser = new CommentUser();
                commentUser.setId(list.get(i).getCommentUserId());
                commentUser.setNickName(list.get(i).getCommentUserName());
                commentUser.setAvatar(list.get(i).getCommentUserAvatar());
                list.get(i).setCommentUser(commentUser);
                CommentUser targetUser = new CommentUser();
                targetUser.setId(list.get(i).getTargetUserId());
                targetUser.setNickName(list.get(i).getTargetUserName());
                targetUser.setAvatar(list.get(i).getTargetUserAvatar());
                list.get(i).setTargetUser(targetUser);
                this.childrenList(list.get(i).getChildrenList());
            }
        }
        return list;
    }

    private SingerCommentRespVo childrenComment(List<SingerCommentRespVo> commentList, SingerCommentRespVo comment) {
        List<SingerCommentRespVo> children = new ArrayList<>();
        for (SingerCommentRespVo m : commentList) {
            if (m.getParentId().equals(comment.getId())) {
                children.add(childrenComment(commentList, m));
            }
        }
        comment.setChildrenList(children);
        return comment;
    }
}
